# Maintainer: Sven-Hendrik Haase <svenstaro@archlinux.org>
# Contributor: t3ddy  <t3ddy1988 "at" gmail {dot} com>
# Contributor: Adrián Chaves Fernández (Gallaecio) <adriyetichaves@gmail.com>
pkgname=0ad
pkgver=a26
_pkgver=0.0.26-alpha
pkgrel=11
pkgdesc="Cross-platform, 3D and historically-based real-time strategy game"
arch=('loong64' 'x86_64')
url="http://play0ad.com/"
license=('GPL2' 'CCPL')
depends=('0ad-data' 'binutils' 'boost-libs' 'curl' 'enet' 'libogg' 'libpng' 'libvorbis'
         'libxml2' 'openal' 'sdl2' 'wxwidgets-gtk3' 'zlib' 'libgl' 'glu' 'fmt'
         'gloox' 'miniupnpc' 'libminiupnpc.so' 'icu' 'nspr' 'libsodium' 'which')
makedepends=('boost' 'cmake' 'mesa' 'zip' 'libsm' 'rust' 'python' 'python-virtualenv' 'git')
options=('!lto') # breaks spidermonkey linking (https://bugs.gentoo.org/746947)
source=("https://releases.wildfiregames.com/$pkgname-$_pkgver-unix-build.tar.xz"
        mozjs_virtualenv.patch
        fix_python_3.11_ftbfs.patch
        https://github.com/0ad/0ad/commit/839edc3a.patch
        https://github.com/0ad/0ad/commit/093e1eb2.patch)
	0ad-fix-build.patch
	0ad-la64.patch)
sha512sums=('aaf647d5d8454c244015d2a198beeaaebc571a5bc96446f3acff8dbd05f9843029c500bf4162651a5e1fcdb42bd5fb5b4f5c512c78372479fbd8565dd093f272'
            '3a0f935ab05e1c0d6a926ba02a5ed72afbb94b6910acaad77661b927680f192a06c7614287fad2ff8a54e3e1ee814614c9abfc9497a27e86b9e58ae1f6eebbfb'
            '748a75420541947e2a215b3a8789a0e137179e4981d0977e1c4b20cd7b86af2d96b9976e04d60ace8d5ee465d542cadc42ee9bceedaaa97d2b320f533e3e3892'
            '1dfc8a0c6ac29040f72d9bbf6b631a74cbdec444b9078a015345139228666354d9b5059f85b640ce3afc0f590bcbe8afd5e158509a0c95751e1cd69fece46876'
            'a7fd1454385f56b7c8cb0fc6ac001761d4419df4aeec570ba846c7df4eb327d25b9ff1a7946cb334315109fa90ca2c1820583619f4e1ec5d53805afa08e10093')

prepare() {
  cd "$pkgname-$_pkgver"

  # Thanks Debian!
  patch -Np1 -i "$srcdir"/fix_python_3.11_ftbfs.patch
  cp "$srcdir"/mozjs_virtualenv.patch libraries/source/spidermonkey
  patch -p1 -i ../839edc3a.patch # Fix build with fmt 10
  patch -p1 -i ../093e1eb2.patch # Fix build with GCC 13

  patch -d libraries/source/spidermonkey -p1 -i $srcdir/0ad-fix-build.patch
  cp $srcdir/0ad-la64.patch libraries/source/spidermonkey/0ad-la64.patch
  echo "patch -p5 < ../0ad-la64.patch" >> libraries/source/spidermonkey/patch.sh

  echo "cp /usr/share/automake-1.16/config.* build/autoconf/" >> libraries/source/spidermonkey/patch.sh
}

build() {
  cd "$pkgname-$_pkgver/build/workspaces"

  # this uses malloc_usable_size, which is incompatible with fortification level 3
  export CFLAGS="${CFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"
  export CXXFLAGS="${CXXFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"

  ./update-workspaces.sh -j$(nproc) \
      --without-pch \
      --bindir=/usr/bin \
      --libdir=/usr/lib/0ad \
      --datadir=/usr/share/0ad/data

  cd gcc

  VERBOSE=1 make
}

package() {
  cd "$pkgname-$_pkgver"
  install -d "${pkgdir}"/usr/{bin,lib/0ad,share/"${pkgname}"/data}
  install -Dm755 binaries/system/pyrogenesis "${pkgdir}/usr/bin"
  install -Dm755 binaries/system/*.so "${pkgdir}/usr/lib/0ad"

  cp -r binaries/data/l10n/ "${pkgdir}/usr/share/${pkgname}/data/"

  install -Dm755 build/resources/${pkgname}.sh "${pkgdir}/usr/bin/${pkgname}"
  install -Dm644 build/resources/${pkgname}.desktop "${pkgdir}/usr/share/applications/${pkgname}.desktop"
  install -Dm644 build/resources/${pkgname}.png "${pkgdir}/usr/share/pixmaps/${pkgname}.png"
}
